<?php
/*由脚本生成 powerBy--Reer*/

namespace app\common\manage;

use app\common\bean\ListMap;
use app\common\bean\SwiperBean;
use app\common\enum\SwiperIsShowEnum;
use app\common\exception\MyException;
use app\common\model\SwiperModel;
use app\common\utils\TimeUtil;
use app\common\validate\SwiperValidate;
use think\Exception;

class SwiperManage extends BaseManage
{
    /**
     * @return SwiperManage
     */
    static function getInstance(){
        return parent::getInstance(); // TODO: Change the autogenerated stub
    }

    /** 获取api轮播图
     * @return array|bool|false
     * @throws \Exception
     */
    public function getApiSwiperList(SwiperBean $swiperBean){
        $where[SwiperBean::$isShow] = SwiperIsShowEnum::SHOW;
        $where[SwiperBean::$storeId] = $swiperBean->getStoreId();
        $result = SwiperModel::getInstance()->selectData($where,true,SwiperBean::$displayOrder);
        $result ?: MyException::throwMyException("获取轮播图失败");
        return $result;
    }

    /**获取分页数据
     * @param ListMap $listMap
     * @return mixed
     * @throws \Exception
     */
    public function getPaginatorList(ListMap $listMap) {
        // TODO: Implement getPaginatorList() method.
        $conditionMap = new ListMap();
        if($timeArray = TimeUtil::getInstance()->dealBetweenTime($listMap)){
            $conditionMap->setParameter(SwiperBean::$createdAt,$timeArray);
        }
        $conditionMap->setParameter(SwiperBean::$isShow,$listMap->getParameter(SwiperBean::$isShow));
        $conditionMap->setParameter(SwiperBean::$storeId,$listMap->getParameter(SwiperBean::$storeId));
        !$this->getParamBool($listMap,"orderFieldName") ?: $conditionMap->setOrder($listMap->getParameter('orderFieldName').SPACE.$listMap->getParameter('orderType'));
        $result = SwiperModel::getInstance()->getList($conditionMap);
        $result ?: MyException::throwMyException("获取失败");
        return $result;
    }

    /**添加数据
     * @param SwiperBean $swiperBean
     * @return mixed
     * @throws Exception
     * @throws \Exception
     */
    public function add($swiperBean ) {
        // TODO: Implement add() method.
        SwiperValidate::getInstance()->goCheck(SwiperValidate::$add,$swiperBean->getData());
        $swiperBean->setCreatedAt(TimeUtil::getInstance()->getTimeNow());
        $result = SwiperModel::getInstance()->addDataGetInsertId($swiperBean->getData());
        $result ?: MyException::throwMyException("添加失败");
        return $result;
    }

    /**编辑数据
     * @param SwiperBean $swiperBean
     * @return mixed
     * @throws Exception
     * @throws \Exception
     */
    public function edit($swiperBean) {
        // TODO: Implement edit() method.
        SwiperValidate::getInstance()->goCheck(SwiperValidate::$edit,$swiperBean->getData());
        $result = SwiperModel::getInstance()->updatedData(array(SwiperBean::$id => $swiperBean->getId(),SwiperBean::$storeId => $swiperBean->getStoreId()),$swiperBean->getData());
        $result ?: MyException::throwMyException("修改失败");
        return $result;
    }

    /**删除数据
     * @param SwiperBean $swiperBean
     * @return mixed
     * @throws Exception
     * @throws \Exception
     */
    public function delete($swiperBean) {
        // TODO: Implement delete() method.
        SwiperValidate::getInstance()->goCheck(SwiperValidate::$pkId,$swiperBean->getData());
        $result = SwiperModel::getInstance()->deleteData(array(SwiperBean::$id => $swiperBean->getId(),SwiperBean::$storeId => $swiperBean->getStoreId()));
        $result ?: MyException::throwMyException("删除失败");
        return $result;
    }

    /**根据主建获取数据
     * @param SwiperBean $swiperBean
     * @return SwiperBean
     * @throws Exception
     * @throws \Exception
     */
    public function getInfo($swiperBean) {
        // TODO: Implement getInfo() method.
        SwiperValidate::getInstance()->goCheck(SwiperValidate::$pkId,$swiperBean->getData());
        $result = SwiperModel::getInstance()->selectData([SwiperBean::$id => $swiperBean->getId(),SwiperBean::$storeId => $swiperBean->getStoreId()]);
        $result ?: MyException::throwMyException("查询失败");
        $swiperBean->setData($result);
        return $swiperBean;
    }
}